#include "led_display.h"
#include "mcu_api.h"
#include "wifi.h"
#include "config.h"
#define WIFI_LED_PORT         GPIOA
#define WIFI_LED_PIN          GPIO_PIN_8

#define LED_SMARTCONFIG_THR   250     
#define LED_AP_THR            1500

#define LED_SMARTCONFIG_BIT        0
#define LED_AP_BIT                 1
#define LED_NOT_CONNECT_WIFI_BIT   2
#define LED_CONNECTED_WIFI_BIT     3
#define LED_CONNECTED_CLOUD_BIT    4
static uint8_t led_status_flag;
static volatile uint16_t led_timer_cnt;
void led_timer_int_handler(void)
{
    led_timer_cnt++;
}

void led_dispaly_init(void)
{
   rcu_periph_clock_enable(RCU_GPIOA);
   gpio_mode_set(WIFI_LED_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, WIFI_LED_PIN);
   gpio_output_options_set(WIFI_LED_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, WIFI_LED_PIN);

   
}

void led_display_mainloop(void)
{
   
   switch(mcu_get_wifi_work_state())
   {
      case SMART_CONFIG_STATE:
         
           if(led_timer_cnt%LED_SMARTCONFIG_THR==0)
           {
              led_status_flag=!led_status_flag;
              if(led_status_flag)
              {
                 GPIO_BOP(WIFI_LED_PORT) = WIFI_LED_PIN;
              }
              else{
                 GPIO_BC(WIFI_LED_PORT) = WIFI_LED_PIN;
              }
           }
      break;
      case AP_STATE:
      
           if(led_timer_cnt%LED_AP_THR==0)
           {
             gd_printf(("here is ap state\r\n"));
              led_status_flag=!led_status_flag;
              if(led_status_flag)
              {
                 GPIO_BOP(WIFI_LED_PORT) = WIFI_LED_PIN;
              }
              else{
                 GPIO_BC(WIFI_LED_PORT) = WIFI_LED_PIN;
              }
           }
      break;
      case WIFI_NOT_CONNECTED:
      //gd_printf(("here is wifi not connect\r\n"));
           GPIO_BC(WIFI_LED_PORT) = WIFI_LED_PIN;
      break;
      case WIFI_CONNECTED:
      case WIFI_CONN_CLOUD:
      //gd_printf(("here is connect cloud\r\n"));
           GPIO_BOP(WIFI_LED_PORT) = WIFI_LED_PIN;
      break;
   }
}